package com.neuqsoft.rd.citizen.thirdapp.sdk;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import com.neuqsoft.api.client.DefaultNeuqClient;
import com.neuqsoft.api.exception.NeuqApiException;
import com.neuqsoft.rd.citizen.thirdapp.sdk.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author baidh
 * @date 2020/12/02
 */
@Service
public class PayService {

    @Autowired
    DefaultNeuqClient client;

    /**
     * 支付下单
     *
     * @param authToken
     * @return
     */
    public NeuqTradePayRsp tradePay(String authToken, String payType, String subject, String totalAmount) throws NeuqApiException {
        NeuqNoticePushReq req = new NeuqNoticePushReq();
        req.setAuthToken(authToken);
        req.setMchId("1000000049");
        req.setMchName("测试商户");
        req.setAppcode("923072C0BC94AB8F22D9D023AEDCA11A");
        req.setIndustry("NORMAL");
        req.setBizNo("test" + System.currentTimeMillis());
        req.setSubject(subject);
        req.setOrderDetail("[]");
        req.setProductType("003");
        req.setPayType(payType);
        req.setYwcode("SMRY");
        req.setTotalAmount(totalAmount);
        req.setSucRedirectUrl("https://www.baidu.com/");
        req.setTimeStart(DateTime.now().toString(DatePattern.PURE_DATETIME_FORMAT));
        req.setTimeExpire(DateTime.now().offset(DateField.MINUTE, 30).toString(DatePattern.PURE_DATETIME_FORMAT));
        req.setTerminalType("001");
        req.setTerminalCode("48-BA-4E-45-7A-C1");
        req.setTerminalPosx("39.942531");
        req.setTerminalPosy("119.586579");
        return client.postJson("https://service.neuqsoft.com/ggfw/citizen/gateway/openapi/api/v1/pay/trade/pay/notice", null, req, "", NeuqTradePayRsp.class);
    }

    /**
     * 支付结果查询
     *
     * @param bizNo
     * @return
     */
    public NeuqOrderStateRsp tradeState(String bizNo) throws NeuqApiException {
        NeuqOrderStateReq req = new NeuqOrderStateReq();
        req.setBizNo(bizNo);
        return client.postJson("https://service.neuqsoft.com/ggfw/citizen/gateway/openapi/api/v1/pay/trade/state", null, req, "", NeuqOrderStateRsp.class);
    }

    /**
     * 支付结果查询
     *
     * @param bizNo
     * @return
     */
    public String tradeRefund(String bizNo, String channelTradeNo) throws NeuqApiException {
        NeuqTradeRefundReq req = new NeuqTradeRefundReq();
        req.setBizNo(bizNo);
        req.setChannelTradeNo(channelTradeNo);
        req.setReason("测试退费");
        req.setOperator("operator1");
        return client.postJson("https://service.neuqsoft.com/ggfw/citizen/gateway/openapi/api/v1/pay/trade/refund", null, req, "", String.class);
    }

}
